﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;
using System.Data;
using System.IO;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminSlide : System.Web.UI.Page
    {
        public Int32 ID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                                            
                    try
                    {
                        ID = Int32.Parse(Request.QueryString["ID"].ToString());

                    }
                    catch (Exception ex)
                    {
                        ID = -1;

                    }               
                if (ID == -1)
                {
                    ViewState["action"] = HangSo.ACTION_ADD;

                }
                else
                {
                    SileShow theSlide = SlideShowProvider.GetSildeByID(ID);

                    if (theSlide == null)
                        Response.Redirect("AdminSlides.aspx");
                    else
                    {
                        ImageSlide.ImageUrl = theSlide.ImageSlide.ToString();
                        ViewState["ID"] = theSlide.ID;
                        ViewState["action"] = HangSo.ACTION_UPDATE;
                    }
                }
               
            }

        }
        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;
            string sExtendsion = ".gif|.GIF|.jpg|.JPG|.png|.PNG";

            try
            {
                if (this.fuPictureUrl.HasFile && sExtendsion.IndexOf(Path.GetExtension(fuPictureUrl.PostedFile.FileName)) < 0)
                {
                    isSuccess = false;
                    message = "Last opp bilde bare.";
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }
            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
             Response.Redirect("AdminSlides.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            if (!ValidateData())
                return;

            Page.Validate();
            if (Page.IsValid)
            {
                SileShow theSlide = new SileShow();
                if (fuPictureUrl.FileName.ToString() != "")
                {
                    if (this.fuPictureUrl.HasFile)
                    {
                        String fileName = fuPictureUrl.PostedFile.FileName;
                        String fileType = Path.GetExtension(fileName);
                        String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                        string str = Server.MapPath("~/");
                        str += "ImageDatas\\Products\\";
                        DirectoryInfo directory = new DirectoryInfo(str);

                        if (!directory.Exists)
                        {
                            DirectoryInfo drt = new DirectoryInfo("\\ImageDatas");
                            if (!drt.Exists)
                            {
                                directory = new DirectoryInfo(Server.MapPath("~/"));

                                directory = directory.CreateSubdirectory("ImageDatas");

                            }
                            directory = new DirectoryInfo(Server.MapPath("~/").ToString() + "\\ImageDatas");
                            directory = directory.CreateSubdirectory("Products");
                        }
                        fuPictureUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Products//" + newFileName);
                        theSlide.ImageSlide = "~//ImageDatas//Products//" + newFileName.Trim();
                    }
                    else
                    {
                        if (action != HangSo.ACTION_ADD)
                        {
                            theSlide.ImageSlide = ViewState["PictureUrl"].ToString();
                        }
                        else
                        {
                            theSlide.ImageSlide = "~//ImageDatas//Products//noImage.gif";
                        }


                    }
                    if (action == HangSo.ACTION_ADD)
                    {
                        theSlide.ID = -1;
                        SlideShowProvider.IUSlideShow(theSlide, true);
                    }
                    else
                    {
                        theSlide.ID = Int32.Parse(ViewState["ID"].ToString());
                        SlideShowProvider.IUSlideShow(theSlide, false);
                    }
                }
                else
                {
                    ltrMessage.Text = "Bildet er ikke null";
                    Response.Redirect("AdminSlide.aspx");
                }
            }
            Response.Redirect("AdminSlides.aspx");

        }
    }
}